import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * Created by leafo on 2017/5/17.
 */
public class SwingDemo extends JFrame {

    public SwingDemo(){
        JPanel root = new JPanel();

        JButton button = new JButton("Button");
        ActionListener i = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                if("Button".equals(actionCommand)){
                    JFileChooser jfc=new JFileChooser();
                    jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                    jfc.showDialog(new JLabel(), "选择");
                    File file=jfc.getSelectedFile();
                    if(file.isDirectory()){
                        System.out.println("文件夹:"+file.getAbsolutePath());
                    }else if(file.isFile()){
                        System.out.println("文件:"+file.getAbsolutePath());
                    }
                    System.out.println(jfc.getSelectedFile().getName());

                }
            }
        };
        button.addActionListener(i);

        root.add(button);
        root.add(new JToggleButton("我是JToggleButton"));
        root.add(new JLabel("我是JLabel"));
        root.add(new JCheckBox("我是JCheckBox"));
        root.add(new JRadioButton("我是JRadioButton"));
        root.add(new JTextField("我是JTextField"));
        root.add(new JPasswordField("我是JPasswordField"));
        root.add(new JTextArea("我是JTextArea"));
        add(root);
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}
